package com.yy.job.boot.service;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yy.job.boot.constants.JobBootConstants;
import com.yy.job.boot.utils.RequestIPUtils;
import com.yy.job.facade.JobFacade;
import com.yy.job.facade.model.YYJobModel;
import com.yy.job.facade.model.YYJobStepModel;

@Service
public class Case1Service {
	
	@Autowired
	private JobFacade jobFacade;
	
	public Long createTrue(HttpServletRequest request, String ip) throws Exception {
		YYJobModel jobModel = new YYJobModel();
		jobModel.setProjectCode(JobBootConstants.PROJECT_CODE);
		jobModel.setModuleCode(JobBootConstants.MODULE2_CODE);
		jobModel.setJobCode("jobCode2");
		jobModel.setBusinessNo("bus_" + new Date().getTime());
		jobModel.setOperator(StringUtils.isNotEmpty(ip) ? ip : RequestIPUtils.getIPAddress(request));
		
		YYJobStepModel task1 = new YYJobStepModel();
		task1.setStepCode("jobTask1");		
		jobModel.addJobStep(task1);
	
		Long submitJob = this.jobFacade.submitJob(jobModel);
		
		return submitJob;
	}
	
	public Long createFalse(HttpServletRequest request, String ip) throws Exception {
		YYJobModel jobModel = new YYJobModel();
		jobModel.setProjectCode(JobBootConstants.PROJECT_CODE);
		jobModel.setModuleCode(JobBootConstants.MODULE2_CODE);
		jobModel.setJobCode("jobCode2");
		jobModel.setBusinessNo("bus_" + new Date().getTime());
		jobModel.setOperator(StringUtils.isNotEmpty(ip) ? ip : RequestIPUtils.getIPAddress(request));
		
		YYJobStepModel task1 = new YYJobStepModel();
		task1.setStepCode("jobTask3");
		task1.setStepParams("id=10");
		
		jobModel.addJobStep(task1);

		Long submitJob = this.jobFacade.submitJob(jobModel);
		
		return submitJob;
	}
	
	public Long createExpected(HttpServletRequest request, String ip) throws Exception {
		YYJobModel jobModel = new YYJobModel();
		jobModel.setProjectCode(JobBootConstants.PROJECT_CODE);
		jobModel.setModuleCode(JobBootConstants.MODULE2_CODE);
		jobModel.setJobCode("jobCode2");
		jobModel.setBusinessNo("bus_" + new Date().getTime());
		jobModel.setOperator(StringUtils.isNotEmpty(ip) ? ip : RequestIPUtils.getIPAddress(request));
		
		int randomInt = RandomUtils.nextInt(5,15);
		jobModel.setExpectedStartTime(DateUtils.addMinutes(new Date(), randomInt));
		
		YYJobStepModel task1 = new YYJobStepModel();
		task1.setStepCode("jobTask3");
		task1.setStepParams("id=10");
		
		jobModel.addJobStep(task1);

		Long submitJob = this.jobFacade.submitJob(jobModel);
		
		return submitJob;
	}
	
}
